package Xiaofang.Homework.H6;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;
import java.util.Scanner;

public class Work1 {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        String path="D:\\Javaoop\\src\\Xiaofang\\Homework\\H6\\products.xml";
        SAXReader reader=new SAXReader();
        Document doc=null;
        try {
            doc=reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        List<Element> list=doc.selectNodes("shop/product");
        System.out.println("1：读取products.xml中的商品信息，输出。");
        for (Element e:list){
            int itemno= Integer.parseInt(e.selectSingleNode("itemno").getText());
            Element name= (Element) e.selectSingleNode("name");
            String sname=name.getText();
            double price= Double.parseDouble(e.selectSingleNode("price").getText());
            int stockcount= Integer.parseInt(e.selectSingleNode("stockcount").getText());
            Element unit= (Element) e.selectSingleNode("unit");
            String sunit=unit.getText();
            System.out.println(itemno+","+sname+","+price+","+stockcount+","+sunit);
        }
        System.out.println("2：查找商品信息：根据输入的商品号，输出对应的商品信息。");
        int item=input.nextInt();
        for (Element e:list){
            if(item==Integer.parseInt(e.selectSingleNode("itemno").getText())){
                int itemno= Integer.parseInt(e.selectSingleNode("itemno").getText());
                Element name= (Element) e.selectSingleNode("name");
                String sname=name.getText();
                double price= Double.parseDouble(e.selectSingleNode("price").getText());
                int stockcount= Integer.parseInt(e.selectSingleNode("stockcount").getText());
                Element unit= (Element) e.selectSingleNode("unit");
                String sunit=unit.getText();
                System.out.println(itemno+","+sname+","+price+","+stockcount+","+sunit);
            }
        }
        System.out.println("3：修改products.xml，输入商品编号（itemno）和数量，修改对应商品的数量（stockcount）；没有找到商品号，给出提示。");
        int item2=input.nextInt();
        System.out.println("-----------");
        int count=input.nextInt();
        for (Element e:list){
            if(item2==Integer.parseInt(e.selectSingleNode("itemno").getText())){
                e.selectSingleNode("stockcount").setText(String.valueOf(count));
                System.out.println(e.selectSingleNode("stockcount").getText());
            }else {
                System.out.println("没有找到商品号");
            }
        }
    }
}
